package com.crm.service.impl.couponManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.couponManager.CouponMapper;
import com.crm.dao.mapper.customMapper.couponCustomManager.CouponCustomMapper;
import com.crm.model.entity.couponManager.Coupon;
import com.crm.model.entity.couponManager.CouponExample;
import com.crm.model.qo.couponManager.CouponQO;
import com.crm.model.vo.couponManager.CouponVO;
import com.crm.service.couponManager.CouponDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:chaifuyou
 * @Date: 2021/5/11 15:55
 */
@Service
public class CouponDbServiceImpl implements CouponDbService {

    @Autowired
    private CouponCustomMapper couponCustomMapper;

    @Autowired
    private CouponMapper couponMapper;

    /**
     * 优惠券分页列表
     * @param couponQO
     * @param order
     * @return
     */
    @Override
    public List<CouponVO> selectFollowPlanList(CouponQO couponQO, String order) {
        return couponCustomMapper.selectFollowPlanList(couponQO,order);
    }

    /**
     * 新增优惠券
     * @param coupon
     */
    @Override
    public void addCoupon(Coupon coupon) {
        couponMapper.insertSelective(coupon);
    }

    /**
     * 修改优惠券信息
     * @param coupon
     */
    @Override
    public void updateCoupon(Coupon coupon) {
        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    /**
     * 根据优惠券id、名称查重
     * @param chsCustomerCouponId
     * @param couponName
     * @return
     */
    @Override
    public Integer selectCountByNameAndId(String chsCustomerCouponId, String couponName) {
        return couponCustomMapper.selectCountByNameAndId(chsCustomerCouponId,couponName);
    }

    @Override
    public List<CouponVO> selectAvailableCouponPageList(CouponQO couponQO) {
        return couponCustomMapper.selectAvailableCouponPageList(couponQO);
    }

    @Override
    public Coupon selectCouponById(String couponId) {
        Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
        return coupon;
    }

    @Override
    public List<Coupon> selectCouponsByIdList(List<String> couponIdList) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        criteria.andChsCustomerCouponIdIn(couponIdList);
        criteria.andCouponStatusEqualTo((short) 0);
        return couponMapper.selectByExample(couponExample);
    }

    @Override
    public List<CouponVO> availableCouponList(CouponQO couponQO) {
        return couponCustomMapper.availableCouponList(couponQO);
    }
}
